<?php
// +----------------------------------------------------------------------
// |
// +----------------------------------------------------------------------
// |
// +----------------------------------------------------------------------
// |
// +----------------------------------------------------------------------
// |
// |
// |
// +----------------------------------------------------------------------
namespace app\common\controller;

use think\Controller;

/**
 * 数据列表自动生成器
 */
class ListBuilder extends Controller
{
    private $_searchItem = [];       //高级搜索参数配置
    private $_tabNav = []; // 页面Tab导航
    private $_tabName = ''; // 页面Tab导航字段
    private $_tableCheckbox = []; // 表格多选框
    private $_tableOperation = []; // 表格操作框
    private $_tableColumn = []; // 表格标题字段
    private $_tableData = []; // 表格数据列表
    private $_extraHtml = []; // 额外功能代码
    private $_dataUrl = '';     //获取数据url
    private $_formUrl = '';     //添加/编辑URL，数据列表按钮
    private $_delUrl = '';      //单行删除URL，数据列表按钮
    private $_delAllUrl = '';   //多行删除URL
    private $_model = '';       //删除等操作Model
    private $_switchTpl = [];   //设置switchTpl模板
    private $_limit = '';       //页数
    private $_page = true;       //是否启用分页
    private $_listBtn = [];     //列表操作按钮组，数据列表按钮
    private $_addBtn = true;      //是否显示添加按钮，表格上部按钮
    private $_delBtn = true;      //是否显示删除按钮，数据列表按钮
    private $_delAllBtn = true;   //是否显示删除全部按钮
    private $_returnBtn = [];   //返回按钮，表格上部按钮
    private $_selfBtn = [];   //自定义按钮，表格上部按钮
    private $_listConfirmBtn = [];   //自定义提示按钮，数据列表按钮
    private $_module = ''; //模块目录
    private $_layerBtn = []; //自定义弹窗按钮，表格上部按钮
    private $_listLayerBtn = []; //自定义弹窗按钮，数据列表按钮
    private $_confirmBtn = []; //自定义确认按钮，表格上部按钮
    private $_fieldSelfBtn = []; //自定义用字段设置样式按钮，表格上部按钮
    private $_batchLayerBtn = []; //自定义批量操作弹窗按钮，表格上部按钮
    private $_selectDataConfirmBtn = []; //自定义选中数据确认按钮，表格上部按钮
    private $_extraForm = []; //添加表单额外参数

    /**
     * 添加搜索
     * @param string $name 字段名称
     * @param string $type 表单类型 select(下拉框)/text（like查询）/range（日期范围）/ between（内容范围）
     * @param string $title 输入框说明
     * @param array $options 表单options 一维数组如[1 => '正常', 0 => '禁用']
     * @param int $unit 数值单位，用于between类型， 结果为：输入值 * $unit
     * @return $this
     */
    public function addSearchItem($name = '', $type = '', $title = '', $options = [], $unit = 1)
    {
        $data                = array(
            'name'    => $name,
            'type'    => $type,
            'title'   => $title,
            'options' => $options ?: [],
            'unit'    => $unit,
        );
        $this->_searchItem[] = $data;
        return $this;
    }

    /**
     * 设置Tab按钮列表
     * @param array $tab_list Tab列表  格式如
     * [
     *    ['key' => '', 'value' => '全部'],
     *    ['key' => '1', 'value' => '显示'],
     *    ['key' => '0', 'value' => '不显示'],
     * ]
     * @return $this
     */
    public function setTabNav($tab_list = [])
    {
        $this->_tabNav = $tab_list;
        return $this;
    }

    /**
     * 设置Tab导航字段
     * @param string $tabName
     * @return $this
     */
    public function setTabName($tabName = 'status')
    {
        $this->_tabName = $tabName;
        return $this;
    }

    /**
     * 添加表格多选框
     *
     */
    public function addTableCheckBox($fixed = '')
    {
        $this->_tableCheckbox = ['type' => 'checkbox', 'fixed' => $fixed];
        return $this;
    }

    /**
     * 添加表格操作框
     * @param int $width 宽度
     * @param string $align 显示格式
     * @return ListBuilder
     */
    public function addTableOperation($width = 160, $fixed = 'right', $align = 'center')
    {
        $this->_tableOperation = ['fixed' => $fixed, 'title' => '操作', 'toolbar' => '#barTool', 'width' => $width, 'align' => $align];
        return $this;
    }

    /**
     * 加一个表格标题字段
     * @param null $field
     * @param $title 表格标题
     * @param int $width //表格宽度
     * @param null $templet //模板渲染
     * @param bool $sort //是否可排序
     * @param string $align //显示格式, left, right, center
     * @param bool $fixed //是否固定列， left, right
     * @param bool $unresize //是否禁用拖拽列宽
     * @param null $edit //是否可以编辑
     * @return ListBuilder
     */
    public function addTableColumn($field = null, $title = null, $width = null, $templet = null, $sort = null, $align = null, $fixed = null, $unresize = null, $edit = null)
    {
        $column               = array(
            'field'    => $field ?: null,
            'title'    => $title ?: null,
            'width'    => $width ?: null,
            'templet'  => $templet ?: null,
            'sort'     => $sort ?: false,
            'align'    => $align ?: 'center',
            'fixed'    => $fixed ?: false,
            'unresize' => $unresize ?: false,
            'edit'     => $edit ?: false,
        );
        $this->_tableColumn[] = $column;
        return $this;
    }

    /**
     * 表格数据列表
     */
    public function setTableDataList($table_data_list)
    {
        $this->_tableData = $table_data_list ?: [];
        return $this;
    }

    /**
     * 设置额外功能代码
     * @param $extra_html 额外功能代码
     * @return $this
     *
     */
    public function setExtraHtml($extra_html, $position = 'bottom')
    {
        $this->_extraHtml[$position] = $extra_html;
        return $this;
    }

    /**
     * 设置获取数据url
     * @param string $url
     * @return ListBuilder
     */
    public function setDataUrl($url = 'index', $params = [])
    {
        $this->_dataUrl = url($url, $params);
        return $this;
    }

    /**
     * 是否显示删除按钮
     * @param bool $is_show
     * @return ListBuilder
     */
    public function setDelBtn($is_show = true)
    {
        $this->_delBtn = $is_show ?: false;
        return $this;
    }

    /**
     * 设置添加/编辑URL
     * @param string $url
     * @return ListBuilder
     */
    public function setFormUrl($url = 'add_edit', $params = [])
    {
        $this->_formUrl = $url ? url($url, $params) : null;
        return $this;
    }

    /**
     * 设置单行删除URL
     * @param string $url
     * @return ListBuilder
     */
    public function setDelUrl($url = 'delete')
    {
        $this->_delUrl = $url ? url($url) : false;
        return $this;
    }

    /**
     * 设置多行删除URL
     * @param string $url
     * @return ListBuilder
     */
    public function setDelAllUrl($url = 'delete')
    {
        $this->_delUrl = $url ? url($url) : false;
        return $this;
    }

    /**
     * 设置删除等操作Model
     * @param string $model
     * @return ListBuilder
     */
    public function setModel($model = '')
    {
        $this->_model = $model;
        return $this;
    }

    /**
     * 设置switchTpl模板
     * @param string $tplId //模板ID
     * @param string $field //字段名
     * @param string $text //显示文本
     * @param string $url //操作URL
     * @return ListBuilder
     */
    public function setSwitchTpl($tplId = '', $field = '', $text = '显示|隐藏', $url = '')
    {
        $data               = [
            'tplId' => $tplId,
            'field' => $field,
            'text'  => $text,
            'url'   => $url ? url($url) : url('admin/Common/setState'),
        ];
        $this->_switchTpl[] = $data;
        return $this;
    }

    /**
     * 设置列表操作按钮组
     * @param array $listBtn 操作按钮组，格式如：[['title' => '按钮名称', 'url' => url('admin/User/add'), 'class' => 'layui-btn-normal']]
     * @return $this
     */
    public function setListBtn($listBtn = [])
    {
        $this->_listBtn = $listBtn;
        return $this;
    }

    /**
     * 设置返回按钮
     * @param null $url
     * @param string $title
     * @return $this
     */
    public function setReturnBtn($url = null, $title = '返回')
    {
        $this->_returnBtn = [
            'url'   => $url,
            'title' => $title
        ];
        return $this;
    }

    /**
     * 设置列表确认按钮
     * @param array $btnArr 按钮数组，格式：[['title' => '按钮名称', 'url' => url('video/DyVideo/sync'), 'class' => '按钮样式', 'msg' => '提示信息'], ['title' => '同步短剧', 'url' => url('video/DyVideo/sync'), 'class' => 'layui-btn-normal', 'msg' => '是否同步短剧到抖音？', 'repet' => 1, 'repet_msg' => '二次提示信息']] repet：是否二次确认，1：是，0：否
     * @return $this
     */
    public function setListConfirmBtn($btnArr = [])
    {
        $this->_listConfirmBtn = $btnArr;
        return $this;
    }

    /**
     * 设置自定义按钮
     * @param array $btnArr 按钮数组，格式：[['title'=>'按钮名称','url'=>url('admin/User/add'),'class'=>'按钮样式']]
     * @return $this
     */
    public function setSelfBtn($btnArr = [])
    {
        $this->_selfBtn = $btnArr;
        return $this;
    }

    /**
     * 设置自定义确认按钮
     * @param array $btnArr 按钮数组，格式：[['title'=>'按钮名称','url'=>url('admin/User/add'),'class'=>'按钮样式', 'msg' => '是否删除全部商品？']]
     * @return $this
     */
    public function setConfirmBtn($btnArr = [])
    {
        $this->_confirmBtn = $btnArr;
        return $this;
    }

    /**
     * 选中数据确认操作按钮
     * @param array $btnArr 按钮数组，格式：[['title'=>'导入','url'=>url('admin/User/import'),'class'=>'layui-btn-normal', 'msg' => '是否导入选中商品？']]
     * @return $this
     */
    public function setSelectDataConfirmBtn($btnArr = [])
    {
        $this->_selectDataConfirmBtn = $btnArr;
        return $this;
    }

    /**
     * 设置弹框按钮
     * @param array $btnArr 按钮数组，格式：[['title'=>'按钮名称','url'=>'按钮链接','class'=>'layui-btn-normal']]
     * @return $this
     */
    public function setLayerBtn($btnArr = [])
    {
        foreach ($btnArr as &$btn) {
            $btn['area'] = empty($btn['area']) ? '["66vw", "80vh"]' : $btn['area'];
        }
        $this->_layerBtn = $btnArr;
        return $this;
    }

    /**
     * 设置批量操作弹框按钮
     * @param array $btnArr 按钮数组，格式：[['title' => '批量设置商品', 'url' => url('batch'), 'class' => 'layui-btn-normal', 'name' => 'form']]
     * @return $this
     */
    public function setBatchLayerBtn($btnArr = [])
    {
        $this->_batchLayerBtn = $btnArr;
        return $this;
    }

    /**
     * 设置列表弹框按钮
     * @param array $btnArr 按钮数组，格式：[['title'=>'按钮名称','url'=>'按钮链接','area' => "['80vw', '90vh']",'class'=>'按钮样式']] 必传字段：title、url、area
     * @return $this
     */
    public function setListLayerBtn($btnArr = [])
    {
        $this->_listLayerBtn = $btnArr;
        return $this;
    }

    /**
     * 设置用字段设置按钮样式的操作组
     * @param array $fieldSelfBtn 操作按钮组，格式如：[['type' => 'form', 'title' => '按钮名称', 'url' => url('admin/User/add'), 'class' => 'layui-btn-normal', 'field' => 'status', 'value' => '0'], ['type' => 'confirm', 'title' => '按钮名称', 'url' => url('admin/User/add'), 'class' => 'layui-btn-normal', 'msg' => '提示信息', 'repet' => 1, 'repet_msg' => '二次提示信息', 'field' => 'status', 'value' => '0']] type：按钮类型，form-表单，confirm-确认框
     * @return $this
     */
    public function setFieldSelfBtn($fieldSelfBtn = [])
    {
        $this->_fieldSelfBtn = $fieldSelfBtn;
        return $this;
    }

    /**
     * 添加表单额外参数
     * @param string $name 字段名称
     * @param string $type 表单类型 select(下拉框)/text（like查询）
     * @param string $title 输入框说明
     * @param array $options 表单options 一维数组如[1 => '正常', 0 => '禁用']
     * @param int $unit 数值单位，用于between类型， 结果为：输入值 * $unit
     * @return $this
     */
    public function addExtraItem($name = '', $type = '', $title = '', $options = [], $unit = 1)
    {
        $data                = array(
            'name'    => $name,
            'type'    => $type,
            'title'   => $title,
            'options' => $options ?: [],
            'unit'    => $unit,
        );
        $this->_extraForm[] = $data;
        return $this;
    }

    /**
     * 是否显示添加按钮
     * @param bool $is_show
     * @return $this
     */
    public function setAddBtn($is_show = true)
    {
        $this->_addBtn = $is_show ?: false;
        return $this;
    }


    /**
     * 是否显示删除全部按钮
     * @param bool $is_show
     * @return $this
     */
    public function setDelAllBtn($is_show = true)
    {
        $this->_delAllBtn = $is_show ?: false;
        return $this;
    }

    /**
     * 设置分页
     * @param bool $page //是否启用分页
     * @return ListBuilder
     */
    public function setPage($page = true)
    {
        $this->_page = $page;
        return $this;
    }

    /**
     * 设置页数
     * @param int $limit //页数
     * @return ListBuilder
     */
    public function setLimit($limit = 10)
    {
        $this->_limit = $limit;
        return $this;
    }

    /**
     * 赋值页面数据
     */
    public function returnForm()
    {
        $indexList['searchItem'] = $this->_searchItem;
        $indexList['tabNav']     = $this->_tabNav;
        $indexList['tabName']    = $this->_tabName;

        $tableColumn = [];
        if ($this->_tableColumn) {
            if ($this->_tableCheckbox) {
                $tableColumn[] = $this->_tableCheckbox;
            }
            foreach ($this->_tableColumn as $val) {
                $tableColumn[] = $val;
            }
            if ($this->_tableOperation) {
                $tableColumn[] = $this->_tableOperation;
            }
        }
        $indexList['tableColumn'] = json_encode([$tableColumn]);

        //赋值列表数据
        $this->assign('indexList', $indexList);
        //获取数据url
        $this->assign('url', $this->_dataUrl);
        //赋值添加/编辑URL
        $this->assign('formUrl', $this->_formUrl);
        //赋值单行删除URL
        $this->assign('delUrl', $this->_delUrl ?: url('admin/Common/delete'));
        //赋值多行删除URL
        $this->assign('delAllUrl', $this->_delAllUrl ?: url('admin/Common/delAll'));
        //赋值删除等操作Model
        $this->assign('model', $this->_model);
        //switchTpl模板字段名
        $this->assign('switchTpl', $this->_switchTpl);
        //设置是否分页
        $this->assign('page', $this->_page);
        //赋值页数
        $this->assign('limit', $this->_limit);
        //设置列表按钮
        $this->assign('listBtn', $this->_listBtn);
        //设置返回按钮
        $this->assign('returnBtn', $this->_returnBtn);
        //设置自定义提示按钮
        $this->assign('listConfirmBtn', $this->_listConfirmBtn);
        //设置自定义按钮
        $this->assign('selfBtn', $this->_selfBtn);
        //是否显示添加按钮
        $this->assign('addBtn', $this->_addBtn);
        //是否显示删除按钮
        $this->assign('delBtn', $this->_delBtn);
        //是否显示全部删除按钮
        $this->assign('delAllBtn', $this->_delAllBtn);
        $this->assign('module', $this->_module);
        //设置弹窗按钮
        $this->assign('layerBtn', $this->_layerBtn);
        $this->assign('batchLayerBtn', $this->_batchLayerBtn);
        //设置列表弹窗按钮
        $this->assign('listLayerBtn', $this->_listLayerBtn);
        $this->assign('confirmBtn', $this->_confirmBtn);
        $this->assign('fieldSelfBtn', $this->_fieldSelfBtn);
        $this->assign('selectDataConfirmBtn', $this->_selectDataConfirmBtn);
        $this->assign('extraForm', $this->_extraForm);
    }

}
